import 'package:boss/provider/binding_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/binding/model/bankAccountType.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/city_picker.dart';
import 'package:boss/ui/widget/date_range_picker_field.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/title_row_button.dart';
import 'package:boss/util/baidu_ai/baidu_ai.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

import 'upload_image.dart';

class BindingBrowser extends StatefulWidget {
  @override
  _BindingBrowserState createState() => _BindingBrowserState();
}

class _BindingBrowserState extends State<BindingBrowser> {
  TextEditingController accountBankCon;
  TextEditingController accountNumberCon;
  TextEditingController idCardNumberCon;

  @override
  void initState() {
    super.initState();

    final provider = Provider.of<BindingProvider>(context, listen: false);

    accountBankCon = TextEditingController(text: provider.payInfoForm.accountBank);
    accountNumberCon = TextEditingController(text: provider.payInfoForm.accountNumber);
    idCardNumberCon = TextEditingController(text: provider.payInfoForm.idCardNumber);
  }

  @override
  void dispose() {
    accountBankCon.dispose();
    accountNumberCon.dispose();
    idCardNumberCon.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.only(left: 30.w, right: 30.w, bottom: 30.w),
            child: Consumer<BindingProvider>(
              builder: (context, bindingProvider, child) {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    _Title('上传银行卡正反面'),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/bank_card_front.png',
                          uploadedImage: bindingProvider.payInfoForm.bankCardPositive,
                          onUploaded: bindingProvider.setBankCardPositive,
                          onPickedImage: (image) async {
                            final res = await BaiduAI.identifyBankCard(image);

                            if (res != null) {
                              accountBankCon.value = TextEditingValue(text: res.result.bankName);
                              accountNumberCon.value = TextEditingValue(text: StringUtil.trim(res.result.bankCardNumber));
                            }
                          },
                        ),
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/bank_card_ground.png',
                          uploadedImage: bindingProvider.payInfoForm.bankCardBack,
                          onUploaded: bindingProvider.setBankCardBack,
                        ),
                      ],
                    ),
                    SelectFormField(
                      title: '银行账户',
                      isRequired: true,
                      items: [
                        SelectItem(title: '对公账号', value: BankAccountType.PUBLIC),
                        SelectItem(title: '对私账户', value: BankAccountType.PRIVATE),
                      ],
                      value: bindingProvider.payInfoForm.accountType,
                      onChanged: (value) {
                        bindingProvider.setAccountType(value);
                      },
                    ),
                    BaseTextFormField(
                      controller: accountBankCon,
                      prefixText: '开户银行',
                      isRequired: true,
                      hintText: '例：工商银行',
                      onChanged: bindingProvider.setAccountBank,
                    ),
                    BaseTextFormField(
                      prefixText: '开户银行全称',
                      isRequired: true,
                      hintText: '例：深圳农村商业银xxx支行',
                      onChanged: bindingProvider.setBankName,
                      initialValue: bindingProvider.payInfoForm.bankName,
                    ),
                    BaseTextFormField(
                      controller: accountNumberCon,
                      prefixText: '银行卡号',
                      isRequired: true,
                      hintText: '请输入银行卡号',
                      onChanged: bindingProvider.setAccountNumber,
                      keyboardType: TextInputType.number,
                      inputFormatters: [
                        MaskedTextInputFormatter(mask: 'xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx', separator: '-'),
                      ],
                    ),
                    TitleRowButton(
                      title: '开户银行所在地',
                      isRequired: true,
                      hintText: '请选择',
                      onTap: () async {
                        var res = await showCityPicker(context, locationCode: bindingProvider.payInfoForm.bankAddressCode);
                        if (res != null) {
                          bindingProvider.setBankAddressCode(res);
                        }
                      },
                      value: bindingProvider.pcaName,
                    ),
                    BaseTextFormField(
                      prefixText: '开户姓名',
                      isRequired: true,
                      hintText: '请输入开户人姓名',
                      onChanged: bindingProvider.setAccountName,
                      initialValue: bindingProvider.payInfoForm.accountName,
                    ),
                    BaseTextFormField(
                      prefixText: '手机号码',
                      isRequired: true,
                      hintText: '请输入可联系手机号码',
                      onChanged: bindingProvider.setPhone,
                      initialValue: bindingProvider.payInfoForm.phone,
                    ),
                    BaseTextFormField(
                      prefixText: '支付宝账号',
                      isRequired: true,
                      hintText: '请输入支付宝账号',
                      onChanged: bindingProvider.setAlipayAccount,
                      initialValue: bindingProvider.payInfoForm.alipayAccount,
                    ),
                    BaseTextFormField(
                      prefixText: '电子邮箱',
                      isRequired: true,
                      hintText: '请输入接收对账单的邮箱地址',
                      onChanged: bindingProvider.setEmial,
                      initialValue: bindingProvider.payInfoForm.email,
                    ),
                    _Title('上传身份证正反面'),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/id_card_front.png',
                          onUploaded: (url) {
                            bindingProvider.setIdCardCopy(url);
                          },
                          uploadedImage: bindingProvider.payInfoForm.idCardCopy,
                          onPickedImage: (image) async {
                            final res = await BaiduAI.identifyIDCard(image, BaiduAI.ID_CARD_SIDE_FRONT);
                            if (res != null) {
                              idCardNumberCon.value = TextEditingValue(text: res.wordsResult.idNumber.words);
                            }
                          },
                        ),
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/id_card_ground.png',
                          uploadedImage: bindingProvider.payInfoForm.idCardNational,
                          onUploaded: (url) {
                            bindingProvider.setIdCardNational(url);
                          },
                          onPickedImage: (image) async {
                            final res = await BaiduAI.identifyIDCard(image, BaiduAI.ID_CARD_SIDE_BACK);
                            if (res != null) {
                              try {
                                final issueTime = DateTime.parse(res.wordsResult.issueTime.words);
                                final expiryTime = DateTime.parse(res.wordsResult.expiryTime.words);
                                bindingProvider.setIdCardStartTime(issueTime);
                                bindingProvider.setIdCardEndTime(expiryTime);
                              } catch (e) {
                                print(e);
                              }
                            }
                          },
                        ),
                      ],
                    ),
                    BaseTextFormField(
                      controller: idCardNumberCon,
                      prefixText: '身份证号码',
                      isRequired: true,
                      hintText: '请输入身份证号码',
                      onChanged: bindingProvider.setIdCardNumber,
                    ),
                    DateRangePickerField(
                      title: '有效期限',
                      onStartChanged: bindingProvider.setIdCardStartTime,
                      onEndChanged: bindingProvider.setIdCardEndTime,
                      startDateTime: bindingProvider.idCardStartTime,
                      endDateTime: bindingProvider.idCardEndTime,
                    ),
                    _Title('上传门店门口与店内环境照'),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/out_door.png',
                          onUploaded: bindingProvider.setStoreEntrancePic,
                          uploadedImage: bindingProvider.payInfoForm.storeEntrancePic,
                        ),
                        UploadImage(
                          width: 335.w,
                          height: 212.w,
                          groundImage: 'assets/image/in_door.png',
                          onUploaded: bindingProvider.setIndoorPic,
                          uploadedImage: bindingProvider.payInfoForm.indoorPic,
                        ),
                      ],
                    ),
                    _Title('上传营业执照正面'),
                    UploadImage(
                      width: 690.w,
                      height: 487.w,
                      groundImage: 'assets/image/business_license.png',
                      onUploaded: bindingProvider.setBusinessLicense,
                      uploadedImage: bindingProvider.payInfoForm.businessLicense,
                    ),
                    if (bindingProvider.payInfoForm.accountType == BankAccountType.PUBLIC)
                      Column(
                        children: <Widget>[
                          _Title('上传用户许可证'),
                          UploadImage(
                            width: 690.w,
                            height: 487.w,
                            groundImage: 'assets/image/business_license.png',
                            onUploaded: bindingProvider.setOpeningPermit,
                            uploadedImage: bindingProvider.payInfoForm.openingPermit,
                          ),
                        ],
                      )
                  ],
                );
              },
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保 存',
          onTap: () {
            var bindingProvider = Provider.of<BindingProvider>(context, listen: false);
            bindingProvider.addOrUpdateMchInfo();
          },
        ),
      ],
    );
  }
}

class _Title extends StatelessWidget {
  final String title;

  const _Title(this.title);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 108.w,
      alignment: Alignment.centerLeft,
      child: RichText(
        text: TextSpan(
          text: title,
          style: TextStyleConstant().normal_1_28,
          children: [
            TextSpan(
              text: '*',
              style: TextStyleConstant().origin_28,
            ),
          ],
        ),
      ),
    );
  }
}
